package com.softer.wxzj.common.flow.util;

import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.flow.enums.FlowAuditTypeEnum;
import com.softer.wxzj.common.flow.enums.FlowStateEnum;
import com.softer.wxzj.entity.FlowConfig;
import com.softer.wxzj.entity.FlowInstance;
import com.softer.wxzj.mapper.FlowConfigMapper;
import com.softer.wxzj.mapper.FlowInstanceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
@Slf4j
public class FlowAddAuditUtil {

    @Autowired
    private FlowConfigMapper flowConfigMapper;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;
    @Autowired
    private FlowAuditUtil flowAuditUtil;
    /**
     * 初始化bean文件
     */
    public static FlowAddAuditUtil flowUtil;

    @PostConstruct
    public void init() {
        flowUtil = this;
        flowUtil.flowConfigMapper = this.flowConfigMapper;
        flowUtil.flowInstanceMapper = this.flowInstanceMapper;
    }

    /**
     * 流程实例初始化
     *
     * @param businessId
     * @param flowType
     * @return true业务状态审核中   false业务状态审核成功
     */
    public String initFlow(String businessId, String flowType, String operNo) {
        log.info("新增信息流程实例初始化开始,业务id:{},流程类型:{}", businessId, flowType);
        // 根据业务id查询正在处于审核中状态的条数
        int count = flowInstanceMapper.getCountByBusinessId(businessId);
        if (count > 0) {
            return null;
        }
        //初始化实例表
        String initState = flowAuditUtil.initInstance(businessId, flowType, null, operNo);
        log.info("新增信息流程实例初始化结束,initState:{}" + initState);
        return initState;
    }

    /**
     * 审核通过
     *
     * @param opinion
     * @return 1
     */
    String passFlow(String opinion, FlowInstance flowInstance, String operNo) {
        log.info("*** 通过 ***");
        String resState = FlowStateEnum.IN_AUDIT.getState();
        List<FlowConfig> config = flowConfigMapper.getInfoCurrAndNextById(flowInstance.getConfigId());
        //config  下标0当前节点   1下一个节点   config.size=1 表示无下节点
        if (NumberEnum.ONE.getNum() >= config.size()) {
            //审批结束
            flowInstanceMapper.updateAuditOver(flowInstance.getId());
            resState = FlowStateEnum.PASS.getState();
        } else {
            //更新审批实例为下一步
            flowAuditUtil.updateFlowInstance(flowInstance, config.get(NumberEnum.ONE.getNum()));
        }
        //保存记录表
        flowAuditUtil.saveFlowAuditdetail(flowInstance, opinion, config.get(NumberEnum.ZERO.getNum()), String.valueOf(NumberEnum.TWO.getNum()), operNo);
        return resState;
    }

    /**
     * 回退  -- 0保存  1审核中 2审核成功  3审核失败   4退回
     *
     * @param opinion
     * @return 1成功
     */
    String backFlow(String opinion, FlowInstance flowInstance, String operNo) {
        log.info("*** 回退 ***");
        String resState = FlowStateEnum.IN_AUDIT.getState();
        //查询当前和上一个节点
        List<FlowConfig> config = flowConfigMapper.getInfoCurrAndUpById(flowInstance.getConfigId());
        //  下标0当前节点   1上一个节点   config.size=1 表示无上节点
        if (1 >= config.size()) {
            //审批结束
            flowInstanceMapper.updateAuditOver(flowInstance.getId());
            resState = FlowStateEnum.BACK.getState();
        } else {
            //更新实例到上一个节点
            flowAuditUtil.updateFlowInstance(flowInstance, config.get(1));
        }
        //保存记录表
        flowAuditUtil.saveFlowAuditdetail(flowInstance, opinion, config.get(0), String.valueOf(NumberEnum.FOUR.getNum()), operNo);
        return resState;
    }

    /**
     * 否决
     *
     * @param opinion
     * @return
     */
    String vetoFlow(String opinion, FlowInstance flowInstance, String operNo) {
        log.info("*** 否决 ***");
        //查询当前节点
        FlowConfig config = flowConfigMapper.getInfoById(flowInstance.getConfigId());
        //审批结束
        flowInstanceMapper.updateAuditOver(flowInstance.getId());
        //保存记录表
        flowAuditUtil.saveFlowAuditdetail(flowInstance, opinion, config, String.valueOf(NumberEnum.THREE.getNum()), operNo);
        return FlowStateEnum.VETO.getState();
    }


    /**
     * 审批类型控制器  0保存  1审核中 2审核成功  3审核失败   4退回
     *
     * @param businessId
     * @param opinion
     * @param mark
     * @return
     */
    public String audit(String businessId, String opinion, String mark, String operNo) {
        log.info("新增审批开始,businessId:{},opinion:{},mark:{}", businessId, opinion, mark);
        String resState = null;
        FlowInstance flowInstanceInfo = flowAuditUtil.getInstance(businessId);
        if (null != flowInstanceInfo) {
            if (FlowAuditTypeEnum.AUDIT_PASS.getType().equals(mark)) {
                resState = passFlow(opinion, flowInstanceInfo, operNo);
            } else if (FlowAuditTypeEnum.AUDIT_VETO.getType().equals(mark)) {
                resState = vetoFlow(opinion, flowInstanceInfo, operNo);
            } else if (FlowAuditTypeEnum.AUDIT_BACK.getType().equals(mark)) {
                resState = backFlow(opinion, flowInstanceInfo, operNo);
            }
        }
        log.info("新增审批结束,resState:{}", resState);
        return resState;
    }
}
